package leetcode.lt100;

import java.util.Stack;

class Solution_71 {
	public static void main(String[] args) {
		simplifyPath("/..");
	}

	public static String simplifyPath(String path) {

		Stack<String> stack = new Stack<String>();
		path = path.replace("//", "/");

		if (path.endsWith("/"))
			path = path.substring(0, path.length() - 1);

		String[] ss = path.split("/");

		for (String s : ss) {
			if (s.equals(".") || s.equals("")) {
				continue;
			} else if (s.equals("..")) {
				if (!stack.isEmpty())
					stack.pop();
			} else {
				stack.push(s);
			}
		}

		StringBuilder res = new StringBuilder();

		Stack<String> temp = new Stack<String>();

		while (!stack.isEmpty())
			temp.push(stack.pop());

		while (!temp.isEmpty())
			res.append("/" + temp.pop());

		if (res.length() == 0) {
			res.append("/");
		}

		return res.toString();
	}
}